<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var arr = [1, 3, 2, 4, 0, 5, 9, 7];
        //循环arr数组得到每个数组项 外层控制交换的遍数
        for (var i = 0; i < arr.length - 1; i++) {
            //内层控制交换多少次
            for (var j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    var temp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        console.log(arr);


        //     1. 获取中间项
        // 2. 声明两个空数组 left right
        // 3. 用中间项做基准 比中间项大的放进right中 比中间项晓得 放进left中
        // 4. 将数组拼起来 left + 中间项 + right

        function QuickSort(arr) {
            if (arr.length <= 1) {
                return arr;
            }
            var num = Math.floor(arr.length / 2);
            var numValue = arr.splice(num, 1)[0];
            var left = [];
            var right = [];
            for (var i = 0; i < arr.length; i++) {
                if (arr[i] < numValue) {
                    left.push(arr[i]);
                } else {
                    right.push(arr[i]);
                }
            }
            return QuickSort(left).concat(numValue, QuickSort(right));
        }
        var arr1 = [4, 5, 2, 7, 8, 4, 6, 2, 1, 9, 0];
        console.log(QuickSort(arr));
    </script>
</body>

</html>